<?php
class Photo extends Vfriend_Db {
	var $cache;
	var $clear;	
	public function __construct()
	{
		$this->cache 		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;
	}
	public function getPhoto($intUserId, $intImageId) {
		try {
			$strKey 	= PHOTO_GET . $intUserId . $intImageId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_get(?,?)');
				$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
				$this->bindParam(2, $intImageId, 	PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Photo::getPhoto - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getAlbumInfo($intAlbumId) {
		try {
			$strKey 	= PHOTO_GETALBUMINFO . $intAlbumId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call ialbums_get(?)');
				$this->bindParam(1, $intAlbumId, PDO::PARAM_INT, 11);
				$arrData 	= $this->fetchAll();
				$this->cache->set($strKey,$arrData,PHOTO_GETALBUMINFO_EXPIRE);
			}
			return isset($arrData[0]) ? $arrData[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Photo::getAlbumInfo - ' . $e->getMessage());
			return null;
		}
	}	
	public function getImageForAlbum($intAlbumId,$intTime=1) {
		try {
			$arrResult	= null;
			$strKey 	= PHOTO_GETIMAGEFORALBUM . $intAlbumId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_getall_topnews(?,?)');
				$this->bindParam(1, $intAlbumId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intTime, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GETIMAGEFORALBUM_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getImageForAlbum - ' . $e->getMessage());
			return null;
		}
	}
	public function unlinkImg($intUserId, $intImageId, $strPath='') {
		$arrImage = $this->getPhoto($intUserId, $intImageId);
		if($strPath == '') {
			if($arrImage) {
				$strFull 	= '';
				$strThumb 	= '';
				$strSmall	= '';
				if($arrImage['type'] == 1) {
					$strFull 	= DIR_AVATAR_FULL . $arrImage['path'];
					$strThumb	= DIR_AVATAR_THUMB . $arrImage['path'];				
					$strSmall	= DIR_AVATAR_SMALL . $arrImage['path'];				
				}
				elseif($arrImage['type'] == 2) {
					$strFull 	= DIR_ALBUM_FULL . $arrImage['path'];
					$strThumb	= DIR_ALBUM_THUMB . $arrImage['path'];		
				}
				if(file_exists($strFull))
					@unlink($strFull);
				if(file_exists($strThumb))
					@unlink($strThumb);
				if(file_exists($strSmall))
					@unlink($strSmall);
			}
		} else {
			$strFull 	= DIR_ALBUM_FULL . $strPath;
			$strThumb	= DIR_ALBUM_THUMB . $strPath;
			$strAFull 	= DIR_AVATAR_FULL . $strPath;
			$strAThumb	= DIR_AVATAR_THUMB . $strPath;				
			$strASmall	= DIR_AVATAR_SMALL . $strPath;		
			if(file_exists($strFull))
				@unlink($strFull);
			if(file_exists($strThumb))
				@unlink($strThumb);
			if(file_exists($strAFull))
				@unlink($strAFull);
			if(file_exists($strAThumb))
				@unlink($strAThumb);
			if(file_exists($strASmall))
				@unlink($strASmall);
		}
	}	
}
?>